<?php

namespace app\index\controller;

use app\BaseController;
use app\index\facade\TestFacade;
use app\index\model\User as UserModel;
use app\middleware\Auth;
use think\App;
use think\exception\ValidateException;
use think\facade\Event;
use think\Request;

class Index extends BaseController
{
    protected $middleware = [Auth::class];

    /**
     * 没有路由定义时原始路由：index.php/Index/index
     */
    /**
     * @param Request $request 中间件传入
     * @param UserModel $userModel 引入
     */
    public function index(Request $request, UserModel $userModel)
    {

        dump('当前应用名:' . app('http')->getName());

        echo "--------------------容器依赖注入-------------------------";
        //容器注入
        dump($userModel->info());
        //自行注入，等于new User()?
        $user = invoke('app\index\model\User');
        dump($user->username);
        //封包注入？
        dump(invoke(function (UserModel $u) {
            dump($u->username);
        }));

        echo "--------------------对象化调用-------------------------------";
        //读取配置文件
        dump(app()->config->get('database'));
        //使用session
        app()->session->set('user_name', '张三');
        dump(app()->session->get('user_name'));

        echo "--------------------服务--------------------------------";
        dump(app()->user->boot());  //app内绑定了user服务实例


        echo "--------------------门面--------------------------------";
        dump(TestFacade::hello('thinkphp'));    //代理类

        echo "--------------------继承BaseController--------------------------------";
        dump($this->app->getBasePath());    //基于base

        echo "--------------------Request--------------------------------";
        dump($this->request->action());             //基于base
        dump($request->hello);                      //中间件
        dump(\think\facade\Request::action());      //静态
        dump(request()->action());          //request函数助手
        dump(input('id'));          //input函数助手

        echo "--------------------validate--------------------------------";

        try {
            $this->validate([
                'name' => 'thinkphp',
                'email' => 'thinkphp@qq',
            ], 'app\index\validate\User');
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            dump($e->getError());
        }

        echo "--------------------Response--------------------------------";
        halt('Response 输出测试');
    }

    /**
     * /hello?name=xxx
     */
    public function hello()
    {
        dump("param，hello " . $this->app->request->param('name'));
        dump("session，hello " . session('name'));
    }

    /**
     * 登录
     * @param UserModel $user
     */
    public function login(userModel $user)
    {
        dump('开始登录.');
        //登录事件调用
        Event::trigger('UserLogin', $user->info());
    }

    /**
     * 退出登录
     * @param UserModel $user
     */
    public function logout(userModel $user)
    {
        dump('退出登录');
        //退出登录事件调用
        Event::trigger('UserLogout', $user->info());
    }

    public function think()
    {
        dump("中间件跳转至 index/think 方法");
    }

    public function redirect()
    {
        dump('重定向');
        return redirect(url('hello', ['name' => 'xxx']))
            ->with('name', 'thinkphp')     //Session闪存  ??好像不行啊，比较耗时暂时不研究了
            ->remember();                               //记住当前地址并重定向
//        return redirect()->restore();                   // 跳回之前的来源地址
    }

    public function json()
    {
        return json(['a' => 'A'])->code(201)->header([
            'Cache-control' => 'no-cache,must-revalidate'
        ])
            ->cookie('name', 'value', 600)
            ->allowCache(false);
    }

    public function download()
    {
        return download('static/1.png', 'my')
            ->expire(300)       // 设置300秒有效期
            ->force(false);     // 是否强制下载
    }
}
